package com.zhaoyan.okgorxjavademo.presenter;


import com.zhaoyan.okgorxjavademo.Contacts;
import com.zhaoyan.okgorxjavademo.Contacts1;
import com.zhaoyan.okgorxjavademo.Login;
import com.zhaoyan.okgorxjavademo.LzyResponse;
import com.zhaoyan.okgorxjavademo.ServerApi;
import com.zhaoyan.okgorxjavademo.model.Main;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by cxy on 2017/12/19.
 */

public class MainPresenter extends RxPresenter<Main.View> implements Main.Presenter {
    @Inject
    public MainPresenter() {

    }

    @Override
    public void detachView() {
        super.detachView();
        unSubscribe();
    }


    @Override
    public void getContacts(String orgCode, String name) {
        addSubscribe(ServerApi.getContacats(orgCode, name).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<LzyResponse<List<Contacts>>>() {
                    @Override
                    public void accept(LzyResponse<List<Contacts>> contacts) throws Exception {
                        mView.getContacatsSuccess(contacts);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.mainFail(throwable);
                    }
                }));
    }

    @Override
    public void getContacts1() {
        addSubscribe(ServerApi.getContacats1("","").observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<Contacts1>() {
                    @Override
                    public void accept(Contacts1 contacts) throws Exception {
                        mView.getContacatsSuccess1(contacts);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.mainFail(throwable);
                    }
                }));
    }

    @Override
    public void login() {
        addSubscribe(ServerApi.login().observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<LzyResponse<Login>>() {
                    @Override
                    public void accept(LzyResponse<Login> contacts) throws Exception {
                        mView.loginSuccess(contacts);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                                                       mView.mainFail(throwable);
                     }
                }));
    }

}
